﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Shopspace.Domains;
using LuKou.Web.Controllers.Base;
using LuKou.Web.BLL.Service;
using System.Web.Mvc;
using System.Web;
using LuKou.Web.BLL;
using LuKou.Common.Services;

namespace LuKou.Web.Controllers.Website
{
    /// <summary>
    /// Ajax操作控制器
    /// </summary>
    public class AjaxController : WebBaseController
    {
        /// <summary>
        /// 获取属性数据
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public ActionResult GetCategory(long cateId, int? gid)
        {
            if (gid.HasValue)
            {
                ViewData["Goods"] = GoodsService.GetGoodsDetail(gid.Value);
            }

            return View(CategoryService.GetCategoryPropList(cateId));
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="filedata"></param>
        /// <returns></returns>
        public ActionResult UploadImgs(HttpPostedFileBase filedata)
        {
            string file_path = "";
            bool isUpload = false;
            //如果有文件上来
            if (filedata != null && filedata.ContentLength > 0)
            {
                byte[] file = new byte[filedata.ContentLength];
                filedata.InputStream.Read(file, 0, filedata.ContentLength);

                file_path = ServiceHelper.Get<IResourcesSvr>("IResourcesSvr").UploadFile(filedata.FileName, file);
                isUpload = true;
            }
            return Json(new { Path = file_path, IsUpload = isUpload });
        }

        public ActionResult GetSKUPrice(long goodsId, String skus)
        {
            GoodsSKU sku = null;
            List<GoodsSKUProp> props = new List<GoodsSKUProp>();
            if (!string.IsNullOrEmpty(skus))
            {
                foreach (var s in skus.Split('|').Select(c => c.Split(',')).ToList())
                {
                    if (s.Length == 2)
                    {
                        props.Add(new GoodsSKUProp() { Pid = Convert.ToInt64(s[0]), Vid = Convert.ToInt64(s[1]) });
                    }
                }
            }

            Goods goods = GoodsService.GetGoodsDetail(goodsId);
            foreach (var goodsSku in goods.SKUs)
            {
                bool isCheck = true;
                foreach (var skuProp in props)
                {
                    if (goodsSku.SKUProps.Find(c => c.Pid == skuProp.Pid && c.Vid == skuProp.Vid)==null)
                    {
                        isCheck = false;
                    }
                }

                if (isCheck)
                {
                    sku = goodsSku;
                    break;
                }
            }

            return Json(sku != null ? new {Price = sku.Price.ToString(), Amount = sku.Amount, OuterNum = sku.OuterNum} : null);
        }
    }
}
